package com.fatcat.easybuy.service.impl;

import com.fatcat.easybuy.exception.BusinessException;
import com.fatcat.easybuy.mapper.EasyBuyNewsMapper;
import com.fatcat.easybuy.pojo.EasyBuyNews;
import com.fatcat.easybuy.service.EasybuyNewsService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
* @author Fatcat
* @description 针对表【easybuy_news】的数据库操作Service实现
* @createDate 2025-09-10 10:23:13
*/
@Service
public class EasyBuyNewsServiceImpl implements EasybuyNewsService {

    @Autowired
    private EasyBuyNewsMapper easybuyNewsMapper;

    @Override
    public PageInfo<EasyBuyNews> getAllNews(Map<String, Object> params) {
        PageHelper.startPage(params);
        List<EasyBuyNews> newsList = easybuyNewsMapper.selectAllNews(params);
        return new PageInfo<>(newsList);
    }

    @Override
    public EasyBuyNews getNewsById(Integer id) {
        return easybuyNewsMapper.selectNewsById(id);
    }


    @Transactional
    @Override
    public boolean addNews(EasyBuyNews easybuyNews) throws BusinessException {
        try {
            return easybuyNewsMapper.insertNews(easybuyNews) > 0;
        }catch (Exception e) {
            throw new BusinessException("新增新闻失败", e);
        }
    }
@Transactional
    @Override
    public boolean updateNews(EasyBuyNews easybuyNews) {
        try {
            return easybuyNewsMapper.updateNews(easybuyNews) > 0;
        } catch (Exception e) {
            throw new BusinessException("更新新闻失败", e);
        }
    }
@Transactional
    @Override
    public boolean deleteNewsById(Integer id) {
        try {
            return easybuyNewsMapper.deleteNewsById(id) >0;
        } catch (Exception e) {
            throw new BusinessException("删除新闻失败", e);
        }
    }
}
